package com.lbn.jfqsc.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.mvp.BasePresenter;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.DensityUtils;
import com.lbn.baseframe.widget.FragmentTabAdapter;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.account.AccountFragment;
import com.lbn.jfqsc.app.home.HomeFragment;
import com.lbn.jfqsc.app.login.LoginActivity;
import com.lbn.jfqsc.app.message.MessageFragment;
import com.lbn.jfqsc.app.recruitment.RecruitmentFragment;
import com.readystatesoftware.viewbadger.BadgeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements IMainView, FragmentTabAdapter.OnRgsExtraCheckedChangedListener {

    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.mark_view)
    View mark;
    private FragmentTabAdapter tabAdapter;
    private List<Fragment> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.setTitle("首页");
    }


    @Override
    protected void onResume() {
        super.onResume();
        ((AccountFragment)list.get(3)).updateUi();
    }

    @Override
    public BasePresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    public void onPresenterCreated() {
        super.onPresenterCreated();
//        getSupportFragmentManager().beginTransaction().replace(R.id.replace_view, HomeFragment.newInstance()).commitAllowingStateLoss();
//        getSupportFragmentManager().beginTransaction().replace(R.id.replace_view, RecruitmentFragment.newInstance()).commitAllowingStateLoss();

        list = new ArrayList<>();
        list.add(HomeFragment.newInstance());
        list.add(MessageFragment.newInstance());
        list.add(RecruitmentFragment.newInstance());
        list.add(AccountFragment.newInstance());
        tabAdapter = new FragmentTabAdapter(this, list, R.id.replace_view, radioGroup);
        tabAdapter.setOnRgsExtraCheckedChangedListener(this);

//        BadgeView badge = new BadgeView(this, mark);
//        badge.setBadgeMargin(DensityUtils.dp2px(this,20),DensityUtils.dp2px(this,2));
//        badge.setText("1");
//        badge.show();

    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

        switch (index) {
            case 0:
                appbar.setTitle("首页");
                break;
            case 1:
                appbar.setTitle("社团");
                break;
            case 2:
                appbar.setTitle("招聘");
                break;
            case 3:
                appbar.setTitle("个人中心");
                ((AccountFragment)list.get(3)).updateUi();
                break;
        }
    }

    public void clearUser(){
        ((RadioButton)radioGroup.getChildAt(0)).setChecked(true);
        tabAdapter.showTab(0);
    }

}
